package main

import (
	"fmt"
	_ "github.com/fvbock/endless"
	"github.com/gin-gonic/gin"
	"naio/config"
	"naio/db/mysql"
	"naio/db/redis"
	_ "naio/docs"
	"naio/router"
	"naio/util"
	"os"
	_ "syscall"
)

func init() {
	appDir := util.GetAppDir()
	config.Init(appDir + "/conf/conf.ini")

	// mysql全局单例
	err := mysql.GetMysqlPool().Setup()
	if err != nil {
		fmt.Printf("start server error: %v\n", err)
		os.Exit(1)
	}

	// 池化redis
	redis.Setup()

}

// @title naio
// @version 1.0
// @description 这是一个基于gin的脚手架应用
// @termsOfService https://gitee.com/1259814898/naio
func main() {
	if config.Config.Env == "prod" {
		gin.SetMode(gin.ReleaseMode)
	}
	application := router.New()
	err := application.GetRouter().Run("0.0.0.0:" + config.Config.HttpListenPort)
	//使用endless热启动
	//maxHeaderBytes := 1 << 20
	//endless.DefaultMaxHeaderBytes = maxHeaderBytes
	//server := endless.NewServer("0.0.0.0:" + config.Config.HttpListenPort, r.GetEngine())
	//server.BeforeBegin = func(add string) {
	//	log.Printf("actual pid is %d", syscall.Getpid())
	//}
	//err := server.ListenAndServe()

	if err != nil {
		fmt.Println("start server error!!")
		os.Exit(0)
	}
}
